package com.zigong.app.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.utils.DateUtils;
import com.zigong.app.R;
import com.zigong.app.beans.ChatItem;
import com.zigong.app.ui.adapter.holder.ChatHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by rh008 on 2017/7/27.
 */

public class ChatRoomAdapter extends BaseHolderAbsAdapter<ChatItem, ChatHolder> {
    private boolean isLiving = false;
    private Comparator<ChatItem> dyComparator = new Comparator<ChatItem>() {
        @Override
        public int compare(ChatItem o1, ChatItem o2) {
            if (o1 != null && o2 != null) {
                if (isLiving) {
                    return DateUtils.getDataDate(o2).compareTo(DateUtils.getDataDate(o1));
                } else {
                    return DateUtils.getDataDate(o1).compareTo(DateUtils.getDataDate(o2));
                }
            }
            return 0;
        }
    };

    public ChatRoomAdapter(Context context, List<ChatItem> list) {
        super(context, list);
    }

    @Override
    public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_chat, parent, false);
        return new ChatHolder(context, view);
    }

    public void setLiving(boolean isLiving) {
        this.isLiving = isLiving;
    }

    public void setDatas(List<ChatItem> list) {
        if (data == null) {
            data = new ArrayList<>();
        }
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public void addDatas(List<ChatItem> list) {
        if (data == null) {
            data = new ArrayList<>();
        }
        data.addAll(0, list);
        Collections.sort(data, dyComparator);
        notifyDataSetChanged();
    }

    public void addDatasToTop(List<ChatItem> list) {
        if (data == null) {
            data = new ArrayList<>();
        }
        data.addAll(0, list);
        notifyDataSetChanged();
    }
}
